var express = require('express');
var router = express.Router();

let login = require('../src/service/loginSer');

let jwt = require('../src/Uilt/jwt')

//登录
router.get('/login', async function (req, res, next) {
  let { user, pwd } = req.query;
  let result = await login.login({ user, pwd });
  if (result) {
    let jwtStr = jwt.createjwt({ username: result.username }, "yy", 30);
    res.json({ code: 0, msg: "登录成功", token: jwtStr });
  } else {
    res.json({ code: 1, msg: "用户名或者密码错误" })
  }

});

//注册管理员
router.post('/addlog/:user/:pwd', async function (req, res, next) {
  let { user, pwd } = req.params;

  let result = await login.loginadd({ user, pwd });
  res.json(result);
});

//查询账户是否存在
router.get('/userfind/:user', async function (req, res, next) {
  let { user } = req.params;
  let result = await login.userfind({ user});
  res.json(result);
});




module.exports = router;
